//
//  GMLoginVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/7/6.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMLoginVC.h"
#import "GMPersonInfo.h"
#import "GMRegisterVC.h"
#import "GMForgotVC.h"
#import "DeviceInfoManager.h"
@interface GMLoginVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *passwdTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation GMLoginVC

//隐藏导航栏
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.accountTF.text = [XGMArchiverTools cacheForKey:USERACCOUNT]?[XGMArchiverTools cacheForKey:USERACCOUNT]:@"";
}
/**登录按钮*/
- (IBAction)loginBtnClick:(id)sender {
    /**使键盘收缩*/
    [self.view endEditing:YES];
    /**判断密码是否为空*/
    if (kIsEmptyString(self.accountTF.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入用户名" toView:self.view];
        return;
    }
    /**判断密码是否为空*/
    if (kIsEmptyString(self.passwdTF.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入密码" toView:self.view];
        return;
    }
    //获取手机型号
    const NSString *mobileModel = [[DeviceInfoManager sharedManager] getDeviceName];
    NSDictionary *dict= @{@"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"account":self.accountTF.text,
                          @"pwd":self.passwdTF.text,
                          @"mobileModel":mobileModel
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"do-api.ashx?$m=AppLogin&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                [MBProgressHUD showErrorOrSuc:@"您已登录成功" toView:self.view];
                NSDictionary *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                [XGMArchiverTools cache:self.accountTF.text ForKey:USERACCOUNT];
                [XGMArchiverTools cache:self.passwdTF.text ForKey:USERPASSWD];
                /**用户id Name 取出来存储，方便使用*/
                [XGMArchiverTools cache:[dataInfo objectForKey:USER_UserId] ForKey:USER_UserId];
                //展示二维码需要
                [XGMArchiverTools cache:[dataInfo objectForKey:USER_Name] ForKey:USER_Name];
                /**退出登录的时候需要调用*/
//                [XGMArchiverTools removeForKey:USERINFO];
                /**延迟两秒之后发送通知，刷新主控制器*/
//                kPostNotificationWithName(KNOTIFICATION_LOGIN);
//                //剔除栈中所有的控制器
//                NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
//                [VCs removeObject:self];
//                [self.navigationController setViewControllers:VCs];
//
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    [self dismissViewControllerAnimated:YES completion:^{
                        
                    }];
               
                });
            }else{
                /**显示错误信息*/
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:self.view];
            }
            
        }else{
            NSLog(@"%@",error);
        }
    }];
}
/**注册按钮*/
- (IBAction)registerBtnClick:(id)sender {
    GMRegisterVC *regVC = [[GMRegisterVC alloc] init];
    [self.navigationController pushViewController:regVC animated:YES];
}
/**忘记按钮*/
- (IBAction)forgotBtnClick:(id)sender {
    GMForgotVC *forgotVC = [[GMForgotVC alloc] init];
    [self.navigationController pushViewController:forgotVC animated:YES];
    
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:self.accountTF]) {
        NSString *phoneStr = [NSString stringWithFormat:@"%@%@", textField.text, string];
        if ([phoneStr length] > 11 && ![string isEqualToString:@""]) {
            textField.text = [phoneStr substringToIndex:11];
            return NO;
        }
    }
    return YES;
}


@end
